﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MemoLib.Tasks;
using MemoLib;
using MemoLib.Entity;

namespace UI
{
    public partial class frmOnceCreateUpdateTask : BaseForm
    {
        public frmOnceCreateUpdateTask()
        {
            InitializeComponent();
        }

        private OnceTaskCard taskCard = null;
        public frmOnceCreateUpdateTask(OnceTaskCard pCard)
        {
            this.taskCard = pCard;
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!CheckInput())
            {
                MessageBox.Show(sbForErrorMsg.ToString());
                sbForErrorMsg.Clear();
                return;
            }
            OnceTaskCard onceTaskCard = null;
            // 只有创建操作需要生成ID和创建日期
            if (this.OpMode == OperationMode.Create)
            {
                onceTaskCard = new OnceTaskCard();
                onceTaskCard.CardID = Int64.Parse(Utility.GenerateID());
                onceTaskCard.CreateDateTime = DateTime.Now;
            }
            else
            {
                onceTaskCard = this.taskCard;
            }
            onceTaskCard.ExecuteType = this.combType.SelectedItem.ToString();
            onceTaskCard.Context = this.txtContext.Text;
            // 如果指定了网址，将会在context中指定网址信息
            onceTaskCard.Context += Utility.GetCombineWebsiteAddresses(this.txtWebsite1.Text, this.txtWebsite2.Text);
            onceTaskCard.ExecuteDateTime = new DateTime(this.calRemind.SelectionStart.Year, this.calRemind.SelectionStart.Month, this.calRemind.SelectionStart.Day,
                    int.Parse(this.txtRemindHour.Text), int.Parse(this.txtRemindMinute.Text), 0);
            onceTaskCard.Note = this.txtNote.Text;

            ExcelOperator op = new ExcelOperator(ConfigOperator.GetExcelPath());
            op.OpenConn();
            if (this.OpMode == OperationMode.Create)
            {
                op.InsertOnceTask(onceTaskCard);
            }
            else
            {
                op.UpdateOnceTaskRowValue(onceTaskCard);
            }
            op.CloseConn();

            this.Close();
        }

        private void frmOnceCreateUpdateTask_Load(object sender, EventArgs e)
        {
            this.combType.SelectedIndex = 0;
            this.txtRemindMinute.Text = "0";

            if (OpMode == OperationMode.Update)
            {
                this.combType.Text = this.taskCard.ExecuteType;
                this.calRemind.SelectionStart = this.taskCard.ExecuteDateTime;
                this.txtRemindHour.Text = this.taskCard.ExecuteDateTime.Hour.ToString();
                this.txtRemindMinute.Text = this.taskCard.ExecuteDateTime.Minute.ToString();
                ContextAndWebsites caw = Utility.SplitContextAndWebsite(this.taskCard.Context);
                this.txtContext.Text = caw.Conterxt;
                this.txtWebsite1.Text = caw.Website;
                this.txtWebsite2.Text = caw.Website2;
                this.txtNote.Text = this.taskCard.Note;
            }
        }

        protected override bool CheckInput()
        {
            bool ret = false;
            ret = NotNullEmpthCheck(this.combType.Text, "操作类型") & NotNullEmpthCheck(this.txtRemindHour.Text, "提醒时间") & 
                NotNullEmpthCheck(this.txtContext.Text, "提醒内容");

            return ret;
        }

        private OperationMode OpMode
        {
            get
            {
                return taskCard == null ? OperationMode.Create : OperationMode.Update;
            }
        }

        private void btnComplete_Click(object sender, EventArgs e)
        {
            OnceTask task = new OnceTask();
            task.Card = this.taskCard;
            task.ComplateTask();

            this.Close();
        }
    }
}
